//
//  MineCollectController.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/12/24.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "MineCollectController.h"
#import "MineCollectCell.h"
#import "UIButton+Extension.h"

@interface MineCollectController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, weak) UIView *bottomView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *selectArray;

/**
 编辑
 */
@property (nonatomic, strong) UIButton *rightBtn;

/**
 删除
 */
@property (nonatomic, strong) UIButton *deleteBtn;

/**
 全选
 */
@property (nonatomic, strong) UIButton *allBtn;

@end

@implementation MineCollectController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品收藏";
    self.view.backgroundColor = WhiteColor;
    
    [self initData];
    
    [self setupChildViews];
    
  
}

#pragma mark - private

- (void)initData {
    for (int i = 0; i < 10; i++) {
        [self.dataArray addObject:[NSNumber numberWithInt:i]];
    }
}

- (void)setupChildViews {
    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = WhiteColor;
    bottomView.hidden = YES;
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0);
        make.bottom.mas_equalTo(IS_IPHONE_X?-20:0);
    }];
    
    UIView *linevvv = [[UIView alloc] init];
    linevvv.backgroundColor = Colordddddd;
    [bottomView addSubview:linevvv];
    [linevvv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    
    UIButton *allSelectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [allSelectButton setTitle:@"全选" forState:UIControlStateNormal];
    [allSelectButton setTitle:@"取消" forState:UIControlStateSelected];
    allSelectButton.titleLabel.font = Font(30);
    [allSelectButton layoutButtonWithImageStyle:ZJButtonImageStyleLeft imageTitleToSpace:10];
    [allSelectButton setTitleColor:Color333333 forState:UIControlStateNormal];
    [allSelectButton setImage:[UIImage imageNamed:@"collect_wei"] forState:UIControlStateNormal];
    [allSelectButton setImage:[UIImage imageNamed:@"collect_selecta"] forState:UIControlStateSelected];
    [bottomView addSubview:allSelectButton];
    [allSelectButton addTarget:self action:@selector(allBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [allSelectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(jisuan(200));
    }];
    self.allBtn = allSelectButton;
    
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setBackgroundImage:[UIImage imageNamed:@"collect_delete"] forState:UIControlStateNormal];
    [bottomView addSubview:deleteButton];
    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.width.mas_equalTo(jisuan(200));
    }];
    [deleteButton addTarget:self action:@selector(deleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.deleteBtn = deleteButton;
    
    self.bottomView = bottomView;
    
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:self.rightBtn]];
    
}

- (void)rightBtnAction {
    self.rightBtn.selected = !self.rightBtn.isSelected;
    self.bottomView.hidden = !self.rightBtn.selected;
    [self.selectArray removeAllObjects];
    [self.tableView reloadData];
    self.allBtn.selected = NO;
    if (self.rightBtn.isSelected) {
        [self.tableView setEditing:YES animated:YES];
    }else{
        [self.tableView setEditing:NO animated:YES];
    }
    
    [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.rightBtn.selected?jisuan(100):0);
    }];
}

- (void)deleteBtnAction {
    NSMutableArray<NSIndexPath *> *indexArr = [NSMutableArray array];
    for (int i = 0; i < self.selectArray.count; i++) {
        NSInteger index = [self.dataArray indexOfObject:self.selectArray[i]];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [indexArr addObject:indexPath];
    }
    
        // 不能写在上面的for循环里面
    
    [self.selectArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([self.dataArray containsObject:obj] ) {
            [self.dataArray removeObject:obj];
        }
    }];
    
    [self.selectArray removeAllObjects];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView deleteRowsAtIndexPaths:indexArr withRowAnimation:UITableViewRowAnimationNone];
    });
}

- (void)allBtnAction {
    self.allBtn.selected = !self.allBtn.isSelected;
    if (self.allBtn.isSelected) {
        NSArray<NSIndexPath *> *arr = self.tableView.indexPathsForVisibleRows;
        [self.selectArray removeAllObjects];
        [self.selectArray addObjectsFromArray:self.dataArray];
        for (NSIndexPath *indexPath in arr) {
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }else{
        [self.selectArray removeAllObjects];
    }
    [self.tableView reloadData];
}

#pragma mark -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCollectCell"];
    cell.multipleSelectionBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
    cell.multipleSelectionBackgroundView.backgroundColor = [UIColor whiteColor];
    
    NSNumber *data = self.dataArray[indexPath.row];
    if ([self.selectArray containsObject:data]) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSNumber *select = self.dataArray[indexPath.row];
    if (![self.selectArray containsObject:select]) {
        [self.selectArray addObject:select];
    }
    self.allBtn.selected = self.selectArray.count == self.dataArray.count ? YES : NO;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSNumber *select = self.dataArray[indexPath.row];
    if ([self.selectArray containsObject:select]) {
        [self.selectArray removeObject:select];
    }
    self.allBtn.selected = self.selectArray.count == self.dataArray.count ? YES : NO;
}


#pragma mark - getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.rowHeight = jisuan(220);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedRowHeight = 0;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [_tableView registerClass:[MineCollectCell class] forCellReuseIdentifier:@"MineCollectCell"];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)selectArray {
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}

- (UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn = [[UIButton alloc] init];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [_rightBtn setTitle:@"完成" forState:UIControlStateSelected];
        
        [_rightBtn setTitleColor:Color333333 forState:0];
        [_rightBtn setTitleColor:Color333333 forState:1];
        
        [_rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [_rightBtn sizeToFit];
    }
    return _rightBtn;
}


@end
